|
1
|
|
|
/** |
|
2
|
|
|
* List of available function: |
|
3
|
|
|
* - formToSky ajaxify-form |
|
4
|
|
|
* - getBlockFromSky Fetch (ajax) function permitting to get block via a POST request |
|
5
|
|
|
* - responsiveImage Transform image's path (src) produce with Liip to responsive path |
|
6
|
|
|
* - convertImgLinkToResponsiveImgLink Transform link to image produce with Liip to responsive Link |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Fetch (ajax) function permitting to get block via a POST request |
|
11
|
|
|
* (prevent from spam) |
|
12
|
|
|
* |
|
13
|
|
|
* @param {string} attribute |
|
14
|
|
|
*/ |
|
15
|
|
|
export function getBlockFromSky(attribute = 'data-sky') |
|
16
|
|
|
{ |
|
17
|
|
|
document.querySelectorAll('[' + attribute + ']').forEach(item => { |
|
18
|
|
|
fetch(item.getAttribute(attribute), { |
|
19
|
|
|
headers: {'Content-Type': 'application/json', Accept: 'text/plain'}, |
|
20
|
|
|
method: 'POST', |
|
21
|
|
|
// Later: maybe implement sending data form data-post |
|
22
|
|
|
// body: JSON.stringify({"contact": (document.getElementById("contact") !== null ? 1: 0)}), |
|
23
|
|
|
credentials: 'same-origin', |
|
24
|
|
|
}) |
|
25
|
|
|
.then(function (response) { |
|
26
|
|
|
return response.text(); |
|
27
|
|
|
}) |
|
28
|
|
|
.then(function (body) { |
|
29
|
|
|
item.removeAttribute('data-sky'); |
|
30
|
|
|
item.innerHTML = body; |
|
31
|
|
|
|
|
32
|
|
|
// add export function to reDo on document dom ready |
|
33
|
|
|
if (typeof onPageLoaded === 'function') { |
|
34
|
|
|
onPageLoaded(); |
|
35
|
|
|
} |
|
36
|
|
|
if (typeof onDomLoaded === 'function') { |
|
37
|
|
|
onDomLoaded(); |
|
38
|
|
|
} |
|
39
|
|
|
}); |
|
40
|
|
|
}); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* ajaxify-form |
|
45
|
|
|
*/ |
|
46
|
|
|
export function formToSky(userOptions = {}) |
|
47
|
|
|
{ |
|
48
|
|
|
var options = { |
|
49
|
|
|
selector: '.ajax-form', // selector for ajax form |
|
50
|
|
|
}; |
|
51
|
|
|
for (var attrname in userOptions) { |
|
52
|
|
|
options[attrname] = userOptions[attrname]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
document.querySelectorAll(options.selector).forEach(item => { |
|
56
|
|
|
if (item.querySelector('form') !== null) { |
|
57
|
|
|
item.querySelector('form').addEventListener('submit', e => { |
|
58
|
|
|
e.preventDefault(); |
|
59
|
|
|
sendFormToSky(e); |
|
60
|
|
|
}); |
|
61
|
|
|
} |
|
62
|
|
|
}); |
|
63
|
|
|
|
|
64
|
|
|
var sendFormToSky = function (form) { |
|
65
|
|
|
var $submitButton = getSubmitButton(form); |
|
66
|
|
|
if ($submitButton !== null) { |
|
67
|
|
|
var initialButton = getSubmitButton(form).outerHTML; |
|
68
|
|
|
$submitButton.outerHTML = '<i class="fa fa-spinner fa-spin"></i>'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
//var formData = new FormData(); |
|
72
|
|
|
var toSend = ''; |
|
73
|
|
|
for (var i = 0; i < form.srcElement.length; ++i) { |
|
74
|
|
|
toSend += |
|
75
|
|
|
encodeURI(form.srcElement[i].name) + |
|
76
|
|
|
'=' + |
|
77
|
|
|
encodeURI(form.srcElement[i].value) + |
|
78
|
|
|
'&'; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
var xmlhttp = new XMLHttpRequest(); |
|
82
|
|
|
xmlhttp.addEventListener( |
|
83
|
|
|
'load', |
|
84
|
|
|
function () { |
|
85
|
|
|
form.srcElement.outerHTML = xmlhttp.responseText; |
|
86
|
|
|
formToSky(); |
|
87
|
|
|
}, |
|
88
|
|
|
false, |
|
89
|
|
|
); |
|
90
|
|
|
xmlhttp.open('POST', form.srcElement.action, false); |
|
91
|
|
|
xmlhttp.setRequestHeader( |
|
92
|
|
|
'Content-type', |
|
93
|
|
|
'application/x-www-form-urlencoded', |
|
94
|
|
|
); |
|
95
|
|
|
xmlhttp.send(toSend); |
|
96
|
|
|
}; |
|
97
|
|
|
|
|
98
|
|
|
var renderError = function (error) { |
|
99
|
|
|
var msg = ''; |
|
100
|
|
|
for (var key in error) { |
|
101
|
|
|
if (error.hasOwnProperty(key)) { |
|
102
|
|
|
var obj = error[key]; |
|
103
|
|
|
for (var prop in obj) { |
|
104
|
|
|
if (obj.hasOwnProperty(prop)) { |
|
105
|
|
|
msg += key + ' : ' + obj[prop] + '<br>'; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
return msg; |
|
111
|
|
|
}; |
|
112
|
|
|
|
|
113
|
|
|
var getSubmitButton = function (form) { |
|
114
|
|
|
if (form.srcElement.querySelector('[type=submit]') !== null) { |
|
115
|
|
|
return form.srcElement.querySelector('[type=submit]'); |
|
116
|
|
|
} |
|
117
|
|
|
if (form.srcElement.getElementsByTagName('button') !== null) { |
|
118
|
|
|
return form.srcElement.getElementsByTagName('button'); |
|
119
|
|
|
} |
|
120
|
|
|
return null; |
|
121
|
|
|
}; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Transform image's path (src) produce with Liip to responsive path |
|
126
|
|
|
* |
|
127
|
|
|
* @param {string} src |
|
128
|
|
|
*/ |
|
129
|
|
|
export function responsiveImage(src) |
|
130
|
|
|
{ |
|
131
|
|
|
var screenWidth = document.clientWidth; |
|
132
|
|
|
if (screenWidth <= 576) { |
|
133
|
|
|
src = src.replace('/default/', '/xs/'); |
|
134
|
|
|
} else if (screenWidth <= 768) { |
|
135
|
|
|
bg_src = src.replace('/default/', '/sm/'); |
|
136
|
|
|
} else if (screenWidth <= 992) { |
|
137
|
|
|
bg_src = src.replace('/default/', '/md/'); |
|
138
|
|
|
} else if (screenWidth <= 1200) { |
|
139
|
|
|
src = src.replace('/default/', '/lg/'); |
|
140
|
|
|
} else { |
|
141
|
|
|
// 1200+ |
|
142
|
|
|
src = src.replace('/default/', '/xl/'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return src; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Transform link to image produce with Liip to responsive Link |
|
150
|
|
|
* |
|
151
|
|
|
* @param {string} attribute |
|
152
|
|
|
* |
|
153
|
|
|
* @example |
|
154
|
|
|
* <a href="/monImage/uneimage.png" data-rimg> |
|
155
|
|
|
* <a href="monimageopti.png"> |
|
156
|
|
|
*/ |
|
157
|
|
|
export function convertImgLinkToResponsiveImgLink(attribute = 'data-rimg') |
|
158
|
|
|
{ |
|
159
|
|
|
var test = []; |
|
160
|
|
|
if (typeof test.push === 'function') { |
|
161
|
|
|
// to avoid gooogle bot execute |
|
162
|
|
|
[].forEach.call( |
|
163
|
|
|
document.querySelectorAll('[' + attribute + ']'), |
|
164
|
|
|
function (element) { |
|
165
|
|
|
element.removeAttribute(attribute); |
|
166
|
|
|
var href = element.getAttribute(href); |
|
167
|
|
|
element.setAttribute('href', responsiveImage(href)); |
|
168
|
|
|
}, |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|